//
//  HOMyExtraAssistantViewController.m
//  WeiDu
//
//  Created by foguo-mac-1 on 26/12/2017.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOMyExtraAssistantViewController.h"
#import "HOButton.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "HOExtraAssistantEntity.h"
#import "HOStaticDataMgr.h"
#import "HOReportListTableViewCell.h"
#import "HOViewStatusViewController.h"
#import "HOReportDetailViewController.h"
#import "HORepaireDoneViewController.h"
#import "HOConfirmOrderViewController.h"
#import "HOAddExtraAssistantViewController.h"
#import "UIImageView+WebCache.h"
#import "HOWebViewController.h"
#import "HOUIMgr.h"

@interface HOMyExtraAssistantViewController ()
{
	UIView 				*statusSelectView;
	UILabel 			*statusLbl;
}
@property(nonatomic, retain)NSMutableDictionary *reportDic;
@property(nonatomic, assign)HO_REPORT_ORDER_STATUS currentStatus;
@property(nonatomic, retain)UIFont *contentFont;
@property(nonatomic, retain)NSString *companyId;
@end

@implementation HOMyExtraAssistantViewController
- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	self.currentStatus = HO_EXTRA_ASS_ORDER_ALL;
	[self initVaribles];
	[self initTableView];
	//	[self initTestData];
	[self requestDataWithMaxId:1];
	[self initPickerView];
	[self initStatusSelectView];
	[self createSegMentController];
	[self createViewExtraStatistics];
}



//创建导航栏分栏控件
-(void)createSegMentController
{
#if 0
	if(![[HOStaticDataMgr sharedInstance] isDirectorUser])
		return;
	NSArray *segmentedArray = [NSArray arrayWithObjects:@"庆春院区",@"下沙院区",nil];
	
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
	
	segmentedControl.frame = CGRectMake(0, 0, 180, 30);
	
	segmentedControl.selectedSegmentIndex = 0;
	segmentedControl.tintColor = [UIColor colorWithRed:252/255.0 green:245/255.0 blue:248/255.0 alpha:1];
	
	[segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
	if([[[HOStaticDataMgr sharedInstance] getCompanyId] isEqualToString:kXiasha])
	{
		[segmentedControl setSelectedSegmentIndex:1];
	}
	else
	{
		[segmentedControl setSelectedSegmentIndex:0];
	}
	[self.navigationItem setTitleView:segmentedControl];
#endif
}

- (void)createViewExtraStatistics
{
	if(![[HOStaticDataMgr sharedInstance] isDirectorUser])
		return;
	UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, self.view.frame.size.height - 150, 50, 50)];
	[btn addTarget:self action:@selector(createViewExtraStatistics:) forControlEvents:UIControlEventTouchUpInside];
	[btn setBackgroundImage:[UIImage imageNamed:@"icon_view_extra"] forState:UIControlStateNormal];
	[self.view addSubview:btn];
}

- (void)createViewExtraStatistics:(id)sender
{
//	HOExtraStatisticsViewController *viewCtrl = [[[HOExtraStatisticsViewController alloc] init] autorelease];
//	[self.navigationController pushViewController:viewCtrl animated:YES];

	HOWebViewController *viewCtrl = [[HOWebViewController alloc] init];
	viewCtrl.hideNavigationBar = NO;
	viewCtrl.navTitle = @"外协单统计";
	NSString *token = [[HOUIMgr sharedInstance] getToken];
	viewCtrl.loadURL = [NSString stringWithFormat:@"%@/mobilepage/ExternalStatistics.html?Token=%@&CompanyId=%@",kServerPath, token,  self.companyId];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
	//我定义了一个 NSInteger tag，是为了记录我当前选择的是分段控件的左边还是右边。
	NSInteger selecIndex = sender.selectedSegmentIndex;
	switch(selecIndex){
		case 0:
//			self.companyId = kQingChun;
			break;
			
		case 1:
//			self.companyId = kXiasha;
		default:
			break;
	}
	[self cleanup];
	
	[self requestDataWithMaxId:1];
	
}

- (void)cleanup
{
	[self.tableViewDataList removeAllObjects];
	[baseTableview reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	if([[HOStaticDataMgr sharedInstance] isDirectorUser])
	{
		[self setNavigationItemTitle:NSLocalizedString(@"我的外协单", @"我的外协单")];
	}
	else
	{
		[self setNavigationItemTitle:NSLocalizedString(@"我的外协单", @"我的外协单")];
	}
	[self requestDataWithMaxId:1];
	baseTableview.frame = CGRectMake(0,
									 statusSelectView.frame.size.height,
									 self.view.frame.size.width,
									 [HOUtils getScreenHeight] - [HOUtils getStatusBarHeight] - 47 - statusSelectView.frame.size.height );
	
	if([[HOStaticDataMgr sharedInstance] getUserInfo].needShowAddExtraBtn)
		[self displayRightButtonItemWithTitle:NSLocalizedString(@"申请协助", @"申请协助")];
}

- (void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
}

- (void)initVaribles
{
	
	self.reportDic = [[[NSMutableDictionary alloc] init] autorelease];
	self.contentFont = [UIFont systemFontOfSize:13];
	if([[HOStaticDataMgr sharedInstance] isDirectorUser])
	{
		self.companyId = [[HOStaticDataMgr sharedInstance] getCompanyId];
	}
}


- (void)initStatusSelectView
{
	statusSelectView = [[UIView alloc]
						initWithFrame:CGRectMake(0,
												 0,
												 self.view.frame.size.width,
												 44)];
	//	statusSelectView.layer.borderWidth = 1;
	[statusSelectView setBackgroundColor:[UIColor whiteColor]];
	NSString *title = [HOUtils getDescribtionWithStatus:self.currentStatus];
	
	UIButton *btn = [[UIButton alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  self.view.frame.size.width,
											  44)];
	btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
	
	//		[btn setTitle:title forState:UIControlStateNormal];
	[btn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xf0f0f0) forView:btn] forState:UIControlStateNormal];
	[btn setTitleColor:TEXT_COLOR_181818 forState:UIControlStateNormal];
	[btn addTarget:self action:@selector(didChangeStatus:) forControlEvents:UIControlEventTouchUpInside];
	[statusSelectView addSubview:btn];
	[btn release];
	
	statusLbl = [[UILabel alloc]
				 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
										  (44 - 22) / 2,
										  200,
										  22)];
	statusLbl.text = title;
	statusLbl.font = [UIFont systemFontOfSize:15];
	statusLbl.textColor = TEXT_COLOR_666666;
	[statusSelectView addSubview:statusLbl];
	
	UIImageView *imgView = [[UIImageView alloc]
							initWithFrame:CGRectMake(self.view.frame.size.width - 16 - HO_HORIZONAL_PADDING,
													 (44 - 9) / 2,
													 16,
													 9)];
	imgView.image = [UIImage imageNamed:@"ic_baoxiuliebiao_zhankai"];
	[statusSelectView addSubview:imgView];
	[imgView release];
	[self.view addSubview:statusSelectView];
	baseTableview.frame = CGRectMake(0,
									 statusSelectView.frame.size.height,
									 self.view.frame.size.width,
									 self.view.frame.size.height - statusSelectView.frame.size.height);
}

- (void)didChangeStatus:(id)sender
{
	[self showPickerView:YES];
}

#pragma mark
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self showPickerView:NO];
	[self hideKeyboard];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 6;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	if(row == 0)
	{
		return NSLocalizedString(@"所有", @"所有");
	}
	else if(row == 1)
	{
		return NSLocalizedString(@"未审核", @"未审核");
	}
	else if(row == 2)
	{
		return NSLocalizedString(@"主任审核通过", @"主任审核");
	}
	else if(row == 3)
	{
		return NSLocalizedString(@"院长审核通过", @"院长审核通过");
	}
	else if(row == 4)
	{
		return NSLocalizedString(@"维修完成", @"维修完成");
	}
	else  if(row == 5)
	{
		return NSLocalizedString(@"确认完成", @"确认完成");
	}
	else  if(row == 6)
	{
		return NSLocalizedString(@"未维修完成", @"未维修完成");
	}
	else
	{
		return NSLocalizedString(@"审核失败", @"审核失败");
	}
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	pickerViewSelectRow = row;
}

- (void)didPickDone:(id)sender
{
	[super didPickDone:nil];
	if(pickerViewSelectRow == 0)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_ALL;
	}
	else if(pickerViewSelectRow == 1)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_UNCHECK;
	}
	else if(pickerViewSelectRow == 2)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_DIR_CHECK;
	}
	else if(pickerViewSelectRow == 3)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_DEAN_CHECK;
	}
	else if(pickerViewSelectRow == 4)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_REPAIRED;
	}
	else if(pickerViewSelectRow == 5)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_CONFIRMED;
	}
	else if(pickerViewSelectRow == 6)
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_CHECK_UNCOMPLETE;
	}
	else
	{
		self.currentStatus = HO_EXTRA_ASS_ORDER_CHECK_FAILED;
	}
	NSString *title = [HOUtils getDescribtionWithStatus:self.currentStatus];
	statusLbl.text = title;
	[self requestDataWithMaxId:0];
}

- (void)requestDataWithMaxId:(int)maxId
{
	if([[HOStaticDataMgr sharedInstance].getUserInfo isLeaderUser] && ![[HOStaticDataMgr sharedInstance] isDirectorUser])
	{
	
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetExternalFaultList,
									  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId],
									  kHO_FIELD_DeanId:[NSNumber numberWithInt:[HOStaticDataMgr sharedInstance].getUserInfo.getUserId],
									  kHO_FIELD_Status:[NSNumber numberWithInt:self.currentStatus]}];
	}
	else{
		
		NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{kHO_FIELD_Action:kHO_ACTION_GetExternalFaultList,
																				   kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId],
																				   kHO_FIELD_Status:[NSNumber numberWithInt:self.currentStatus]}];
		if(self.companyId)
		{
			[dic setObject:self.companyId forKey:kHO_FIELD_CompanyID];
		}
		[self sendRequestWithParams:dic];
	}
	
}

- (void)pullUpToRefresh
{
	HOExtraAssistantEntity *entity = [self.tableViewDataList lastObject];
	if(entity)
	{
		[self requestDataWithMaxId:entity.orderId];
	}
	else
	{
		[self requestDataWithMaxId:1];
	}
}

- (void)pullDownToRefresh
{
	[self requestDataWithMaxId:1];
}

- (void)rightButtonItemDidClick
{
	HOAddExtraAssistantViewController *viewCtrl = [[[HOAddExtraAssistantViewController alloc] init] autorelease];
	viewCtrl.companyId = self.companyId;
	[self.navigationController pushViewController:viewCtrl animated:YES];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.row == 0)
	{
		HOExtraAssistantEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];

		CGFloat width = tableView.frame.size.width - 69;
		CGSize addressSize = [HOUtils findHeightForText:[HOUtils getValideString:entity.address] havingWidth:width andFont:self.contentFont];
		CGSize contentSize = [HOUtils findHeightForText:[entity getFaultDetailContent] havingWidth:width andFont:self.contentFont];
		CGFloat height = 26 + 26 + 4 + 8 + 15 + addressSize.height + contentSize.height;
		return height;
		
	}
else if(indexPath.row == 1)
	return 44;
	return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return HO_HORIZONAL_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOExtraAssistantEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];
	
	if(indexPath.row == 0)
	{
		static NSString *ReportTableViewCellIdentifier = @"ReportTableViewCellIdentifier2";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReportTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]init] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;

		UIImageView *imgView = (UIImageView *)[cell viewWithTag:111];
		if(imgView == nil)
		{
			imgView = [[UIImageView alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING, HO_HORIZONAL_PADDING, 48, 48)];
//			imgView.layer.borderWidth = 1;
			[cell.contentView addSubview:imgView];
			imgView.tag = 111;
		}
		NSString *imageStr = @"";
		if([entity.imageList count] > 0)
		{
			imageStr = [entity.imageList firstObject];
		}
		NSURL *imgURL = [NSURL URLWithString:imageStr];
		[imgView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"morentu"] options:SDWebImageCacheMemoryOnly];
		
		
		UILabel *statusLbl = (UILabel *)[cell viewWithTag:113];
		if(statusLbl == nil)
		{
			statusLbl = [[UILabel alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING * 2 + imgView.frame.size.width,
																   HO_HORIZONAL_PADDING,
																   tableView.frame.size.width - 58 - 10,
																   22)];
			//			statusLbl.layer.borderWidth = 1;
			statusLbl.textColor = TEXT_COLOR_666666;
			statusLbl.font = [UIFont systemFontOfSize:15];
			[cell.contentView addSubview:statusLbl];
			statusLbl.tag = 113;
		}
		statusLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"状态:" withContent:[entity getStatusString] withColor:[HOUtils getOrderColorWithStatus:entity.status]];
		
		
		UILabel *orderIdLbl = (UILabel *)[cell viewWithTag:112];
		if(orderIdLbl == nil)
		{
			orderIdLbl = [[UILabel alloc] initWithFrame:CGRectMake(statusLbl.frame.origin.x,
																   statusLbl.frame.origin.y + statusLbl.frame.size.height + 2,
																   tableView.frame.size.width - imgView.frame.size.width - 3 * HO_HORIZONAL_PADDING,
																   22)];
//			orderIdLbl.layer.borderWidth = 1;
			orderIdLbl.textColor = TEXT_COLOR_666666;
			orderIdLbl.font = [UIFont systemFontOfSize:12];
			[cell.contentView addSubview:orderIdLbl];
			orderIdLbl.tag = 112;
		}
		
		
		orderIdLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"单号:" withContent:[NSString stringWithFormat:@"%d", entity.orderId] withColor:nil];


		UILabel *timeLbl = (UILabel *)[cell viewWithTag:114];
		if(timeLbl == nil)
		{
			timeLbl = [[UILabel alloc] initWithFrame:CGRectMake(statusLbl.frame.origin.x,
																orderIdLbl.frame.origin.y + orderIdLbl.frame.size.height + 2,
																tableView.frame.size.width - 58 - 10,
																22)];
//			timeLbl.layer.borderWidth = 1;
			timeLbl.textColor = TEXT_COLOR_666666;
//			timeLbl.textAlignment = NSTextAlignmentRight;
			timeLbl.font = [UIFont systemFontOfSize:12];
			[cell.contentView addSubview:timeLbl];
			timeLbl.tag = 114;
		}
		timeLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"申请时间:" withContent:[HOUtils getDateFromTimeInterval:entity.createTime * 1000]  withColor:nil];

		
		
		UILabel *locationLbl = (UILabel *)[cell viewWithTag:115];
		if(locationLbl == nil)
		{
			locationLbl = [[UILabel alloc] initWithFrame:CGRectMake(timeLbl.frame.origin.x,
																timeLbl.frame.origin.y + timeLbl.frame.size.height + 2,
																	tableView.frame.size.width - imgView.frame.size.width - 3 * HO_HORIZONAL_PADDING,
																22)];
//			locationLbl.layer.borderWidth = 1;
			locationLbl.textColor = TEXT_COLOR_666666;
			locationLbl.font = [UIFont systemFontOfSize:12];
			[cell.contentView addSubview:locationLbl];
			locationLbl.numberOfLines = 2;

			locationLbl.tag = 115;
		}
		locationLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"故障位置:" withContent:entity.address withColor:nil];

		[locationLbl sizeToFit];
		
		UILabel *contentLbl = (UILabel *)[cell viewWithTag:116];
		if(contentLbl == nil)
		{
			contentLbl = [[UILabel alloc] initWithFrame:CGRectMake(locationLbl.frame.origin.x,
																	locationLbl.frame.origin.y + locationLbl.frame.size.height + 2,
																   tableView.frame.size.width - imgView.frame.size.width - 3 * HO_HORIZONAL_PADDING,
																	22)];
//			contentLbl.layer.borderWidth = 1;
			contentLbl.textColor = TEXT_COLOR_666666;
			contentLbl.font = [UIFont systemFontOfSize:14];
			contentLbl.numberOfLines = 2;
			[cell.contentView addSubview:contentLbl];
			contentLbl.textColor = [UIColor blackColor];
			contentLbl.tag = 116;
		}
		if([entity getFaultDetailContent].length > 0)
		{
			contentLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"故障描述:" withContent:[entity getFaultDetailContent]  withColor:nil];
		}
		else
		{
			contentLbl.attributedText = [HOUtils getAttributedStringWithTitle:@"" withContent:[entity getFaultDetailContent]  withColor:nil];
		}
		[contentLbl sizeToFit];
		return cell;
		
	}
	else if(indexPath.row == 1)
	{
		static NSString *HOReportListTableViewCell = @"HOReportListTableViewCell";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOReportListTableViewCell];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOReportListTableViewCell] autorelease];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		
		HOButton *confirmBtn = (HOButton *)[cell viewWithTag:116];
		if(confirmBtn == nil)
		{
			confirmBtn = [[[HOButton alloc]
						   initWithFrame:CGRectMake([HOUtils getScreenWidth] - 80 * 2 - HO_HORIZONAL_PADDING * 2,
													7,
													80,
													30)] autorelease];
			
			confirmBtn.tag = 116;
			[confirmBtn setTitle:NSLocalizedString(@"确认完成", @"确认完成") forState:UIControlStateNormal];
			confirmBtn.layer.borderWidth = [HOUtils defaultOnePixelConversion];
			confirmBtn.layer.borderColor = TEXT_COLOR_009BD3.CGColor;
			[confirmBtn setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
			[confirmBtn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xcccccc) forView:confirmBtn] forState:UIControlStateSelected];
			[confirmBtn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xcccccc) forView:confirmBtn] forState:UIControlStateHighlighted];
			confirmBtn.layer.cornerRadius = 5;
			[confirmBtn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
			confirmBtn.titleLabel.font = [UIFont systemFontOfSize:13];
			[cell.contentView addSubview:confirmBtn];
		}
		confirmBtn = (HOButton *)[cell viewWithTag:116];
		confirmBtn.data = entity;
		int myUserId =  [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId];
		if(entity.status == HO_REPORT_ORDER_ACCEPTED
		   && [[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser]
		   && entity.doneId == myUserId)
		{
			[confirmBtn setTitle:NSLocalizedString(@"维修完成", @"维修完成") forState:UIControlStateNormal];
			confirmBtn.hidden = NO;
		}
		else if(entity.status == HO_REPORT_ORDER_REPAIRED && entity.postId == myUserId)
		{
			[confirmBtn setTitle:NSLocalizedString(@"确认完成", @"确认完成") forState:UIControlStateNormal];
			confirmBtn.hidden = NO;
		}
		else
		{
			confirmBtn.hidden = YES;
		}
		
		HOButton *viewStatusBtn = (HOButton *)[cell viewWithTag:115];
		if(viewStatusBtn == nil)
		{
			
			viewStatusBtn = [[[HOButton alloc]
							  initWithFrame:CGRectMake([HOUtils getScreenWidth] - 80 - HO_HORIZONAL_PADDING,
													   7,
													   80,
													   30)] autorelease];
			
			
			[viewStatusBtn setTitle:NSLocalizedString(@"查看进度", @"查看进度") forState:UIControlStateNormal];
			
			viewStatusBtn.layer.borderWidth = [HOUtils defaultOnePixelConversion];
			viewStatusBtn.layer.borderColor = TEXT_COLOR_666666.CGColor;
			viewStatusBtn.titleLabel.font = [UIFont systemFontOfSize:13];
			viewStatusBtn.layer.cornerRadius = 5;
			viewStatusBtn.tag = 115;
			[viewStatusBtn setTitleColor:TEXT_COLOR_666666 forState:UIControlStateNormal];
			[viewStatusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
			[viewStatusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
			[viewStatusBtn addTarget:self action:@selector(viewStatus:) forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:viewStatusBtn];
		}
		viewStatusBtn = (HOButton *)[cell viewWithTag:115];
		viewStatusBtn.data = entity;
		return cell;
	}
	return nil;
}

- (void)confirm:(HOButton *)sender
{
	HOExtraAssistantEntity *orderEntity = (HOExtraAssistantEntity *)sender.data;
	int myUserId =  [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId];
	if(orderEntity.status == HO_REPORT_ORDER_ACCEPTED && [[[HOStaticDataMgr sharedInstance] getUserInfo] isRepairUser])
	{
		HORepaireDoneViewController *viewCtrl = [[HORepaireDoneViewController alloc] init];
		viewCtrl.orderId = orderEntity.orderId;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else if(orderEntity.status == HO_REPORT_ORDER_REPAIRED && orderEntity.postId == myUserId)
	{
		if(orderEntity.doneId == 0)
		{
			[self popupHintView:NSLocalizedString(@"此报修单还未维修完成，暂不能确认完成", @"此报修单还未维修完成，暂不能确认完成")];
			return;
		}

		HOConfirmOrderViewController *viewCtrl = [[HOConfirmOrderViewController alloc] init];
		viewCtrl.orderEntity = orderEntity;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
}


- (void)viewStatus:(HOButton *)sender
{
	HOViewStatusViewController *viewCtrl = [[HOViewStatusViewController alloc] init];
	viewCtrl.orderEntity = sender.data;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOAddExtraAssistantViewController *viewCtrl = [[HOAddExtraAssistantViewController alloc] init];
	viewCtrl.isDetailDisplayModal = YES;
	HOExtraAssistantEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];
	viewCtrl.extraAssistantEntity = entity;
	viewCtrl.companyId = entity.companyId;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
	
}
#pragma mark - -- tool
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	if([action isEqualToString:kHO_CMD_Report_List_Changed]
	   || [action isEqualToString:kHO_CMD_Report_Detail_Changed])
	{
		[self requestDataWithMaxId:1];
	}
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetExternalFaultList])
	{
		[self.tableViewDataList removeAllObjects];
		[self.reportDic removeAllObjects];
		[baseTableview clearsContextBeforeDrawing];
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			int orderId = [[dic objectForKey:kHO_FIELD_OrderId] intValue];
			HOExtraAssistantEntity *entity = [self.reportDic objectForKey:[NSNumber numberWithInt:orderId]];
			if(entity == nil)
			{
				entity = [[[HOExtraAssistantEntity alloc] init] autorelease];
				[self.reportDic setObject:entity forKey:[NSNumber numberWithInt:orderId]];
				[self.tableViewDataList addObject:entity];
			}
			
			[entity parseDic:dic];
//			entity.image = [dic objectForKey:kHO_FIELD_FaultImage];
		}
		[self doneLoadingTableViewRefresh];
	}
}

- (void)dealloc
{
	[self.reportDic removeAllObjects];
	self.reportDic = nil;
	[super dealloc];
}

@end
